using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using MiddlewareClient;

namespace WarShips
{
    public partial class battleships : Form, IClient_rpt
    {
        Int16 cntr = 0;
        IClient_cmd mClient;
        public battleships()
        {
            InitializeComponent();
            mClient = new MiddlewareClient.MiddlewareClient(this);

            seaBattle1.putShipsOnMap();
            seaBattle1.ControlMode = SeaBattleControl.SeaBattleMap.eSeaBattleControlMode.Mode_TacticMap;

            seaBattle2.ControlMode = SeaBattleControl.SeaBattleMap.eSeaBattleControlMode.Mode_FireConsole;

            //Button initalized to Connect state
            buttonConnect.Tag = true;
        }

        private void seaBattle2_Load(object sender, EventArgs e)
        {

        }

        private void buttonConnect_Click(object sender, EventArgs e)
        {
            if ((Boolean)buttonConnect.Tag)
            {
                Regex regEx = new Regex(@"^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$");
                String ipAddr = textBoxIpAddr.Text;

                if (regEx.IsMatch(ipAddr))
                {
                    textBoxIpAddr.BackColor = Color.LightGreen;
                    //Call in middleware to conect to server
                    mClient.connect(ipAddr);

                    labelStatus.Text = "Connecting...";

                    //disable textboxes
                    textBoxNickName.Enabled = false;
                    textBoxIpAddr.Enabled = false;

                    //enable fire button if success on connectResult
                }
                else
                {
                    textBoxIpAddr.BackColor = Color.Red;
                }
            }
            else //Disconnect
            {
                mClient.disconnect();
            }
        }

        private void buttonReady_Click(object sender, EventArgs e)
        {
            if (seaBattle1.PosStatus)
            {
                if (validateNickName())
                {
                    buttonConnect.Enabled = true;
                    labelStatus.Text = "Ready.";
                }
            }
            else
            {
                buttonConnect.Enabled = false;
                labelStatus.Text = "Invalid ship pos";
            }
        }

        private void buttonFire_Click(object sender, EventArgs e)
        {
            Point p = seaBattle2.getSelectedSquare();
            Console.WriteLine(" fire: {0}", p.ToString());

            mClient.shoot((uint)p.X,(uint)p.Y);
           /* seaBattle2.updateHits(p, cntr++);

            if (cntr > 8)
                cntr = 0;
            * */
        }

        private Boolean validateNickName()
        {
            Regex regEx = new Regex(@"^[a-zA-Z0-9]{3,10}$");
            String nickname = textBoxNickName.Text;
            Boolean retVal = false;

            if (regEx.IsMatch(nickname))
            {
                retVal = true;
            }
            else
            {
                labelStatus.Text = "Wrong nickname";
                MessageBox.Show("Nickname should meet regex - [a-zA-Z0-9]{3,10} \n :->>> ", "Nickname error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            return retVal;
        }

        #region IClient_rpt Members

        public void connectResult(eReturnErrorCode a_eRetCode)
        {
            Console.WriteLine("[C]::CONNECTRESULT RES {0}", a_eRetCode);
            switch(a_eRetCode)
            {
                case eReturnErrorCode.Success:
                {
                    buttonFire.Enabled = true;
                    buttonConnect.Tag = false;
                    labelStatus.Text = "Connected";
                    buttonConnect.Text = "Disconnect";
                    break;
                }
                default:
                {
                    //TODO: error trace or add handling
                    break;
                }
            }
        }

        public void disconnectResult(eReturnErrorCode a_eRetCode)
        {
            Console.WriteLine("[C]::DISCONNECTRESULT RES {0}", a_eRetCode);
            switch (a_eRetCode)
            {
                case eReturnErrorCode.Success:
                    {
                        buttonFire.Enabled = false;
                        buttonConnect.Tag = true;
                        labelStatus.Text = "Disconnected";
                        buttonConnect.Text = "Connect";
                        break;
                    }
                default:
                    {
                        //TODO: error trace or add handling
                        break;
                    }
            }
        }

        public void readyResult(eReturnErrorCode a_eRetCode)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        public void shootResult(uint a_uiXPos, uint a_uiYPos, uint a_uiShooted)
        {
            Point p = seaBattle2.getSelectedSquare();
            seaBattle2.updateHits(p, (short)a_uiShooted);
            //throw new Exception("The method or operation is not implemented.");
        }

        #endregion
    }
}